var flag = false;
function setflag() {
	flag = true;
}
function cancelflag() {
	flag = false;
}
var p= null;
$(function(){
	var fragment = document.createDocumentFragment();
	var fragmentDivbuil = document.createDocumentFragment();
	var fragmentDivhouse = document.createDocumentFragment();
//	var relaJson = ${str};
	for(var i = 0; i < commNameJson.length; i ++){
		//创建div
		var ele = document.createElement("div");
		ele.setAttribute("id", "meanTemp"+i);
		ele.setAttribute("name", i);
		ele.style.width="50px";
		ele.style.height="50px";
		ele.style.position="absolute";
		var funtDivbuil = document.createElement("div");
		funtDivbuil.setAttribute("id", "funtDivbuil"+i);
//		funtDivbuil.style.position="absolute";
		var funtDivhouse = document.createElement("div");
		funtDivhouse.setAttribute("id", "funtDivhouse"+i);
		funtDivhouse.style.position="absolute";
		//代码片段中添加div
		fragment.appendChild(ele);
		fragmentDivbuil.appendChild(funtDivbuil);
		fragmentDivhouse.appendChild(funtDivhouse);
	}
	//body里添加代码片段，也可以替换成需要的element
	document.body.appendChild(fragment);
	document.body.appendChild(fragmentDivbuil);
	document.body.appendChild(fragmentDivhouse);
	p = $("#community").position();
	for(var i = 0; i < commNameJson.length; i++){
		$("#meanTemp"+i).css('border-radius','50px');
		$("#meanTemp"+i).css('background-color','#FFFFFF');
		$("#meanTemp"+i).css('z-index','1');
		$("#meanTemp"+i).css('opacity','0.7');
		$("#meanTemp"+i).css('border','3px solid rgb(112, 208, 207)');
		$("#meanTemp"+i).css('background-color','rgb(255, 255, 255)');
		$("#meanTemp"+i).css('cursor','pointer');//鼠标移动到此div上指针变小手
		$("#meanTemp"+i).offset({
			top : p.top + commNameJson[i].tempXY[0] - 28,
			left : p.left + commNameJson[i].tempXY[1] - 43,
		});
		$("#funtDivbuil"+i).css('z-index','1');
		$("#funtDivbuil"+i).css('text-align','center');
		$("#funtDivbuil"+i).css('height','0px');
		$("#funtDivbuil"+i).css('width','40px');
		$("#funtDivbuil"+i).css('margin','7px auto');
		$("#funtDivbuil"+i).css('font-size','13px');
		$("#meanTemp"+i).append($("#funtDivbuil"+i));
		$("#funtDivbuil"+i).html(commNameJson[i].alias);
//		$("#meanTemp"+i).append($("#XY"));
		$("#funtDivhouse"+i).css('funt-size','2px');
		$("#funtDivhouse"+i).css('top','25px');
		$("#funtDivhouse"+i).css('left','-5px');
		$("#funtDivhouse"+i).css('width','60px');
		$("#funtDivhouse"+i).css('height','0px');
		$("#funtDivhouse"+i).css('text-align','center');
		$("#funtDivhouse"+i).css('z-index','1');
		$("#funtDivhouse"+i).css('color','red');
		$("#funtDivhouse"+i).css('font-size','13px');
		$("#meanTemp"+i).append($("#funtDivhouse"+i));
		for(var j = 0; j < builDate.length; j++){
			if(commNameJson[i].community == builDate[j].buildingName){
				if(houseDate[j] != null){
					if(houseDate[j].houseValue.length != 0){
						var houseValue = houseDate[j].houseValue;
						$("#funtDivhouse"+i).html("" + parseFloat(houseValue).toFixed(1) + "" + "℃");
					}
				}else{
					$("#funtDivhouse"+i).html("0℃");
				}
			}
		}
		//添加点击事件
		$("#meanTemp"+i).live("click",function(event){
			showThisDiv();//点击之后显示热量表
			//从后台获取到点击之后的小区下的所有楼栋，根据获取到的长度，挨个添加给table+i，而后点击table+i之后mark就显示楼栋名称和温度
			var name = event.target.getAttribute("name");
			var commJson = [];
			for(var j = 0; j < commNameJson.length; j++){
				commJson.push(commNameJson[j].community)
			}
			// 鼠标定位
			event = event || window.event;
			var thisDivs = thisDiv.style; 
			var thisTables = thisTable.style; 
			var posX = event.clientX;
			var posY = event.clientY;
			if (posY > 620) {
				thisDivs.top = posY - 100 + 'px';
				thisTables.top = posY - 100 + 'px';
			} else {
				thisDivs.top = posY + 10 + 'px';
				thisTables.top = posY + 10 + 'px';
			}
			if (posX > 610) {
				if($("#showThisTable").css("display")==='block'){
					thisDivs.left = posX - 400 + 'px';
					thisTables.left = posX - 140 + 'px';
				}else if($("#showThisTable").css("display")==='none'){
					thisDivs.left = posX - 120 + 'px';
				}
			} else {
				if(posX < 100){
					thisDivs.left = posX - 80 + 'px';
					thisTables.left = posX + 180 + 'px';
				}else{
					thisDivs.left = posX - 120 + 'px';
					thisTables.left = posX + 140 + 'px';
				}
			}
		});
	}
});
function hidetip() {
	if(!flag){
		thisDiv.style.allowTipHover = true;
		thisTable.style.allowTipHover = true;
		thisDiv.style.display = "none";
		thisTable.style.display = "none";
	}
}
function showThisDiv(){
	thisDiv.style.display = "block";
	thisTable.style.display = "block";
}
$(function() {
	$("#thisDiv").mouseenter(setflag);
	$("#thisDiv").mouseleave(cancelflag);
	$("#thisTable").mouseenter(setflag);
	$("#thisTable").mouseleave(cancelflag);
});